/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.xpath.view;

import java.net.URL;
import java.util.HashMap;
import offset.nodes.Constants;
import offset.nodes.client.model.AppletUtil;
import offset.nodes.client.model.DialogContext;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.view.BaseApplet;
import offset.nodes.client.view.SimpleDialog;
import offset.nodes.client.xpath.view.XPathPanel;

/**
 *
 * @author Walter Lütgenau
 */
public class XPathApplet extends BaseApplet {

    URL service = null;
    URL repository = null;
    String path = null;

    /** Creates a new instance of NewNodeApplet */
    public XPathApplet() {
    }

    public String getPath() {
        return path;
    }

    public URL getService() {
        return service;
    }

    @Override
    public void initUI() {
        try {
            service = new URL(getParameter("service"));
            repository = new URL(getParameter("base"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        path = getParameter("path");

        DialogContext.setInstance(DialogContext.EDIT);
    }

    String getParent(String path) {
        int index = path.lastIndexOf("/");
        if (index < 0)
            return path;
        return path.substring(0, index);
    }

    @Override
    public void startUI() {
        // show dialog
        XPathPanel panel = new XPathPanel(new ServerModel(getService()));
        SimpleDialog dialog = new SimpleDialog(new javax.swing.JFrame(), true, panel);
        dialog.setLocationByPlatform(true);
        dialog.setSize(555, 720);
        dialog.setVisible(true);
        int result = dialog.getReturnStatus();
        dialog.dispose();

        try {
            URL page = null;
            
            if (result == SimpleDialog.OK) {
                String query = panel.getXPathQuery();
                
                HashMap<String,String[]> parameters = new HashMap<String,String[]>();
                parameters.put(Constants.PAR_CONTENT_TYPE, new String[] { Constants.PARVALUE_SEARCH });
                parameters.put(Constants.PAR_QUERY, new String[] { query });
                parameters.put(Constants.PAR_QUERY_TYPE, new String[] { Constants.PARVALUE_XPATH });
                
                page = new URL(repository.toString() + getPath() + AppletUtil.createQuery(parameters, "UTF-8"));
            }
            else
                page = new URL(repository.toString() + getPath());

            // update page
            getAppletContext().showDocument(page);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
